#include <mutex>
#include <condition_variable>
#include <functional>

using namespace std;

class FooBar
{
private:
    int n;
    bool flag = true;
    mutex m;
    condition_variable cv;

public:
    FooBar(int n)
    {
        this->n = n;
    }

    void foo(function<void()> printFoo)
    {
        for (int i = 0; i < n; i++)
        {
            unique_lock<mutex> l(m);
            cv.wait(l, [this]
                    { return this->flag; });
            printFoo();
            flag = false;
            cv.notify_all();
        }
    }

    void bar(function<void()> printBar)
    {
        for (int i = 0; i < n; i++)
        {
            unique_lock<mutex> l(m);
            cv.wait(l, [this]
                    { return !this->flag; });
            printBar();
            flag = true;
            cv.notify_all();
            // printBar() outputs "bar". Do not change or remove this line.
        }
    }
};